package top.rainf.support;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.Random;

/**
 * @author 星环
 * @date 2023/5/20 0:47:14
 * @description
 */
@Slf4j
public class TokenGenerator {
    private static final int SALT = 300;
    static Random random = new Random(System.currentTimeMillis());
    public static String genToken(int uid,String pwd){
        //将用户的id每位加上23
        int salt_id = uid + SALT;
        String id = String.valueOf(salt_id);
        StringBuilder sb = new StringBuilder();
        //将改变后的id  salt_id 加入密码的随机位置
        int randStart = random.nextInt(pwd.length()-1)+1;
        sb.append(pwd);
        sb.insert(randStart,id);
        //把randstart改为正确的位置
        System.out.println("未打乱时 : " + sb.toString());
        //再把sb隔位打乱

        sb.append('%').append(randStart + SALT)
                .append("$").append(id.length());
        for (int i = 0; i < sb.length()/2; i++) {
            char ch = sb.charAt(i*2);
            sb.setCharAt(i*2,sb.charAt(i*2 + 1));
            sb.setCharAt(i*2+1,ch);
        }
        for (int i = 0; i < sb.length(); i++) {
            sb.setCharAt(i, (char) (sb.charAt(i) + SALT));
        }
        String token = sb.toString();
        log.info("为id是{}的用户生成一个token：{}",uid,token);
        return token;
    }
    public static int getUserIdFromToken(String token){
        if(token == null){
            return -1;
        }
        StringBuilder sb = new StringBuilder(token);
        for (int i = 0; i < sb.length(); i++) {
            sb.setCharAt(i, (char) (sb.charAt(i) - SALT));
        }
        for (int i = 0; i < sb.length()/2; i++) {
            char ch = sb.charAt(i*2);
            sb.setCharAt(i*2,sb.charAt(i*2 + 1));
            sb.setCharAt(i*2+1,ch);
        }
        //取得加盐后id长度
        String lenPre = sb.substring(sb.lastIndexOf("$") + 1);
        int idLen = 0;
        try {
            idLen = Integer.parseInt(lenPre);
        }catch (NumberFormatException e){
            e.printStackTrace();
            return 0;
        }
        sb.setLength(sb.length() - lenPre.length() - 1);
        //id位置
        String startPos = sb.substring(sb.lastIndexOf("%") + 1);
        int realStartPos = 0;
        try {
            realStartPos = Integer.parseInt(startPos) - SALT;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0;
        }
        System.out.println("id_start是" + realStartPos);
        //把sb重置
        sb.setLength(sb.lastIndexOf("%"));

        System.out.println(sb.toString());
        //现在是密码和id加盐混合
        String sid = sb.substring(realStartPos, realStartPos + idLen);
        try {
            return Integer.parseInt(sid) - SALT;
        }catch (NumberFormatException e){
            e.printStackTrace();
            return -1;
        }
    }
    @Test
    public void fun(){
        String str = genToken(12, "jdiwqdinqwnfqwi");
        System.out.println(str);
        int uid = getUserIdFromToken(str);
        System.out.println(uid);
    }

}
